/*
 * CadastrodeEstacao.java
 *
 * Created on 26 de Novembro de 2008, 16:22
 */
package br.faimg.pomar.visual;

import br.faimg.pomar.controle.BusinessDelegate.ProdutoBusinessDelegate;
import br.faimg.pomar.controle.componentevisual.ComboTipodeProduto;
import br.faimg.pomar.controle.componentevisual.ItemTipo;
import javax.swing.JComboBox;

/**
 *
 * @author  diogo
 */
public class CadastrodeProdutoInclusao extends javax.swing.JDialog {

    /** Creates new form CadastrodeEstacao */
    public CadastrodeProdutoInclusao() {
        initComponents();

        incluirItensCombo();
        codigoTextField.setEnabled(false);
        cancelarButton.setText("Limpar");
        
    }
    /**
     * Construtor para alteracao de items
     * @param codigo
     * @param descricao 
     * @param tipoCodigo
     * @param qtdEmb
     */
    public CadastrodeProdutoInclusao(String codigo, String descricao, String tipoCodigo, String qtdEmb) {
        initComponents();
        incluirItensCombo();
        
        codigoTextField.setText(codigo);
        descricaoTextField.setText(descricao);
        qtdEmbjTextField.setText(qtdEmb);
        codigoTextField.setEnabled(false);
        okButton.setText("Alterar");
        cancelarButton.setText("Limpar");

        
        //Seleciona o item que foi clicado na tabela.
        for (int i = 0; i < tipoCombo.getItemCount(); i++) {

            if (tipoCodigo.equals(
                    ((ItemTipo) tipoCombo.getItemAt(i)).getCodigo())) {
                tipoCombo.setSelectedIndex(i);
                break;
            }
            
            
        }
    }
    
    
    private void incluirItensCombo() {
        //Inlui os itens no combobox do tipo de estacao
        ComboTipodeProduto itens = new ComboTipodeProduto();
        JComboBox temp = new JComboBox(itens.getList());
        tipoCombo.setModel(temp.getModel());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        descricaoTextField = new javax.swing.JTextField();
        codigoTextField = new javax.swing.JTextField();
        tipoCombo = new javax.swing.JComboBox();
        cancelarButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        qtdEmbjTextField = new javax.swing.JTextField();

        setTitle("Cadastro de Produto");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("Codigo: ");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 20, -1, 20));

        jLabel2.setText("Descrição: ");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 60, -1, -1));

        jLabel3.setText("Tipo: ");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 20, -1, 20));

        descricaoTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                descricaoTextFieldActionPerformed(evt);
            }
        });
        descricaoTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                descricaoTextFieldKeyPressed(evt);
            }
        });
        jPanel1.add(descricaoTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 60, 530, 20));

        codigoTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                codigoTextFieldActionPerformed(evt);
            }
        });
        codigoTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                codigoTextFieldKeyPressed(evt);
            }
        });
        jPanel1.add(codigoTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 20, 50, -1));

        tipoCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tipoComboActionPerformed(evt);
            }
        });
        jPanel1.add(tipoCombo, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 20, 420, 20));

        cancelarButton.setText("Cancelar");
        cancelarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarButtonActionPerformed(evt);
            }
        });
        jPanel1.add(cancelarButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(540, 100, 90, -1));

        okButton.setText("Incluir");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        jPanel1.add(okButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(430, 100, 90, -1));

        jLabel4.setText("Qtd Emb: ");
        jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 90, -1, -1));

        qtdEmbjTextField.setName(""); // NOI18N
        qtdEmbjTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                qtdEmbjTextFieldActionPerformed(evt);
            }
        });
        jPanel1.add(qtdEmbjTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 90, 60, -1));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 660, 130));

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed

    ProdutoBusinessDelegate delegate = new ProdutoBusinessDelegate();

    if (okButton.getText().equals("Alterar")){
        delegate.update(codigoTextField.getText(),
            ((ItemTipo) tipoCombo.getSelectedItem()).getCodigo(),
            descricaoTextField.getText(),
            Integer.parseInt(qtdEmbjTextField.getText()), "");
    }else {
    
        delegate.create(codigoTextField.getText(),
            ((ItemTipo) tipoCombo.getSelectedItem()).getCodigo(),
            descricaoTextField.getText(),
            Integer.parseInt(qtdEmbjTextField.getText()), "");
    }

    //Limpa os combos apos a insercao para permitir nova insercao
    codigoTextField.setText("");
    descricaoTextField.setText("");
    qtdEmbjTextField.setText("");

    this.setVisible(false);
}//GEN-LAST:event_okButtonActionPerformed

private void cancelarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarButtonActionPerformed
    if (codigoTextField.getText().equals("") && descricaoTextField.getText().equals("")) {

        this.setVisible(false);


    } else {
        if(okButton.getText().equals("Alterar")){
            
            if(qtdEmbjTextField.getText().equals("") && descricaoTextField.getText().equals("")){
                this.setVisible(false);
            }
        codigoTextField.setEnabled(false);
        descricaoTextField.setText("");
        qtdEmbjTextField.setText("");
        cancelarButton.setText("Cancelar");
        }


    }
}//GEN-LAST:event_cancelarButtonActionPerformed

private void tipoComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tipoComboActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_tipoComboActionPerformed

private void codigoTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_codigoTextFieldActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_codigoTextFieldActionPerformed

private void descricaoTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_descricaoTextFieldActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_descricaoTextFieldActionPerformed

private void codigoTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_codigoTextFieldKeyPressed
    cancelarButton.setText("Limpar");
}//GEN-LAST:event_codigoTextFieldKeyPressed

private void descricaoTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_descricaoTextFieldKeyPressed
    codigoTextFieldKeyPressed(evt);
}//GEN-LAST:event_descricaoTextFieldKeyPressed

private void qtdEmbjTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_qtdEmbjTextFieldActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_qtdEmbjTextFieldActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelarButton;
    private javax.swing.JTextField codigoTextField;
    private javax.swing.JTextField descricaoTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton okButton;
    private javax.swing.JTextField qtdEmbjTextField;
    private javax.swing.JComboBox tipoCombo;
    // End of variables declaration//GEN-END:variables
}
